/*
 * @Author: Kang
 * @Date: 2021-11-24 17:53:31
 * @Last Modified by: Kang
 * @LastEditTime: 2022-02-12 11:15:00
 */
'use strict';

module.exports = app => {
  class UserService extends app.Service {

    // 查询用户列表
    * index() {
      try {
        const user = yield this.ctx.model.Users.findAll();
        const result = {};
        result.data = user;
        return result;
      } catch (error) {
        throw new Error(error);
      }
    }

    // 创建一个用户
    * create(request) {
      try {
        const user = yield this.ctx.model.Users.create(request);
        const result = {};
        result.data = user;
        return result;
      } catch (error) {
        throw new Error(error);
      }
    }

    // 修改一个用户信息
    * update(id, request) {
      try {
        const user = yield this.ctx.model.Users.findByPk(id);
        const res = yield user.update({
          name: request.name,
          age: request.age,
        });
        const result = {};
        result.data = res;
        return result;
      } catch (error) {
        throw new Error(error);
      }
    }

    // 删除一个用户
    * destroy(id) {
      try {
        const user = yield this.ctx.model.Users.findByPk(id);
        const res = yield user.destroy();
        const result = {};
        result.data = res;
        return result;
      } catch (error) {
        throw new Error(error);
      }
    }
  }
  return UserService;
};
